package cn.yfxu.server.controller;

import cn.yfxu.server.pojo.Menu;
import cn.yfxu.server.pojo.MenuRole;
import cn.yfxu.server.pojo.Result;
import cn.yfxu.server.pojo.Role;
import cn.yfxu.server.service.IMenuRoleService;
import cn.yfxu.server.service.IMenuService;
import cn.yfxu.server.service.IRoleService;
import cn.yfxu.server.service.impl.MenuServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author Xxxxyf
* @date 2021/3/9
* @desc 权限组
*/
@RestController
@RequestMapping("/system/basic/permiss")
public class PermissionController {
    private IRoleService roleService;
    private IMenuService menuService;
    private IMenuRoleService menuRoleService;

    public PermissionController(IRoleService roleService,IMenuService menuService,IMenuRoleService menuRoleService){
        this.roleService=roleService;
        this.menuService=menuService;
        this.menuRoleService=menuRoleService;
    }

    @ApiOperation(value="获取所有角色")
    @RequestMapping(method = RequestMethod.GET,value = "/")
    public List<Role> getAllRoles(){
        return roleService.list();
    }
    @ApiOperation(value="添加角色")
    @RequestMapping(method = RequestMethod.POST,value = "/role")
    public Result addRole(@RequestBody Role role){
        if (!role.getName().startsWith("ROLE_")){
            role.setName("ROLE_"+role.getName());
        }
        if (roleService.save(role)){
            return Result.success("添加成功！");
        }
        return Result.success("添加失败！");
    }
//    @ApiOperation(value="更新角色")
//    @RequestMapping(method = RequestMethod.PUT,value = "/")
//    public Result updateRole(@RequestBody Role role){
//        if (!role.getName().startsWith("ROLE_")){
//            role.setName("ROLE_"+role.getName());
//        }
//        if (roleService.updateById(role)){
//            return Result.success("更新成功！");
//        }
//        return Result.success("更新失败！");
//    }
    @ApiOperation(value="删除角色")
    @RequestMapping(method = RequestMethod.DELETE,value = "/role/{rid}")
    public Result deleteRoleById (@PathVariable Integer rid){
        if (roleService.removeById(rid)){
            return Result.success("删除成功！");
        }
        return Result.success("删除失败！");
    }
    @ApiOperation(value="查询所有菜单")
    @RequestMapping(method = RequestMethod.GET,value = "/menus")
    public List<Menu> getAllMEnus (){
       return menuService.getAllMenus();
    }
    @ApiOperation(value="根据角色id查询菜单id")
    @RequestMapping(method = RequestMethod.GET,value = "/mid/{rid}")
    public List<Integer> getMidByRid (@PathVariable Integer rid){
       return  menuRoleService.list(new QueryWrapper<MenuRole>().eq("rid",rid)).stream().map(MenuRole::getMid).collect(Collectors.toList());
    }
    @ApiOperation(value="更新角色菜单")
    @RequestMapping(method = RequestMethod.PUT,value = "/")
    public Result updateMemuRole (Integer rid,Integer[] mids){
        return  menuRoleService.updateMemuRole(rid,mids);
    }

}
